<?php

namespace Addons\OaSystem\Controller;

use Think\ApiBaseController;

class ApiAttendanceController extends ApiBaseController
{
    var $openid;
    var $appid;
    var $cur_emp_id;
    var $initial_check_pass;
    public function __construct() {
        $this->initial_check_pass = false;
        parent::__construct ();
        $this->openid = get_openid();
        $this->appid = get_app_info()['appid'];
        if($this->openid == -1){
            $return['errcode'] = '40003';
            echo api_return ( $return['errcode'],null,error_msg( $return) );
            exit;
        }
        $map['userid'] = $this->mid;
        $userEmp = M('OaUserEmployee')->where($map)->find();
        if(empty($userEmp)){
            $return['errcode'] = '50001';
            echo api_return ( $return['errcode'],null,error_msg( $return,"找不到对应的员工号") );
            exit;
        }
        $this->cur_emp_id = $userEmp['emp_id'];
        $this->initial_check_pass = true;
    }
    public function GetCheckPoint() {
        if ($this->initial_check_pass)
        $data = M('OaCheckPoint')->where(array('appid'=>$this->appid))->select();
        echo api_return (0,$data);
    }
    public function LaunchWorkOverTime() {
        if(!$this->initial_check_pass) {
            return;
        }
        $data = json_decode(I('apply_data'),true);
        $map['id'] = $data['type'];
        $type = M('OaApplyType')->where($map)->find();
        $dep_map['id'] = $data['dept_id'];
        $dep = M('OaDept')->where($dep_map)->find();
        if (empty($type) || empty($dep)) {
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return));
        }

        $res = create_one_application($data,$type,$dep,$this->cur_emp_id,$this->appid,$apply_id);
        if($res === true){
            $apply_map['id'] = $apply_id;
            $attendceApply = M('OaApplication')->where($apply_map)->find();
            echo api_return( 0,$attendceApply);
        }else{
            echo api_return( $res['errcode'],$res['data'],$res['errmsg']);
        }
    }

    public function LaunchLeaveTime() {
        if(!$this->initial_check_pass) {
            return;
        }
        $data = json_decode(I('apply_data'),true);
        $map['id'] = $data['type'];
        $type = M('OaApplyType')->where($map)->find();
        $dep_map['id'] = $data['dept_id'];
        $dep = M('OaDept')->where($dep_map)->find();
        if (empty($type) || empty($dep)) {
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return));
        }
        $res = create_one_application($data,$type,$dep,$this->cur_emp_id,$this->appid,$apply_id);
        if($res === true){
            $apply_map['id'] = $apply_id;
            $leaveApply = M('OaApplication')->where($apply_map)->find();
            echo api_return( 0,$leaveApply);
            /*
            $cur_step = $leaveApply['step'];
            $attendceApply['step'] = $leaveApply['step'] + 1;
            $go_next = 1;
            $comment='提交';
            $formId = null;
            $res = process_reimburse_apply($leaveApply,$cur_step,$go_next,$comment,$this->cur_emp_id,$this->appid,$formId);
            if($res !== true){
                echo api_return( $res['errcode'],$res['data'],$res['errmsg']);
            }else{
                $apply_map['id'] = $leaveApply['id'];
                $ret_data = M('OaApplication')->where($apply_map)->find();
                echo api_return( 0,$ret_data);
            }
            */
        }else{
            echo api_return( $res['errcode'],$res['data'],$res['errmsg']);
        }
    }

    private function chooseFlow (&$apply_data) {
        $empInfo = M('OaEmployee')->where(array('id'=>$this->cur_emp_id))->find();
        $dept = D('OaDeptView')->where(array('id'=>$empInfo['dept_id']))->find();
        $tar_deptId =  findTargetDetp($dept,2)['id'];
        $flows = M('OaProcessFlow')->where(array('type'=>$apply_data['type']))->select();  // 加班流程
        $flowid = 0;

        foreach ($flows as $flow) {
            $flowAttr = json_decode($flow['attr'],true);
            // var_dump($flowAttr,$tar_deptId);
            if ($flowAttr['dept'] == intval($tar_deptId)) {
                if (empty($flowAttr['ke_dept']) && ($dept['grade_value'] < 3)) {
                    $flowid = $flow['id'];
                    break;
                }else if (($flowAttr['ke_dept'] ==  $dept['id']) && ($dept['grade_value'] == 3)) {
                    $flowid = $flow['id'];
                    break;
                }
            }
        }
        if (empty($flowid)) {
            return false;
        }
        $apply_data['flow_id'] = $flowid;
        return $flowid;
    }
    private function find_first_step($operations,$applyer) {
        $empInfo = M('OaEmployee')->where(array('id'=>$applyer))->find();
        $dept = D('OaDeptView')->where(array('id'=>$empInfo['dept_id']))->find();
        $dutyId = $empInfo['duty_id'];
        // $deptId = $dept['id'];
        // $tar_deptId =  findTargetDetp($dept,2);

        $dutyLvl = M('OaDuty')->where(array('id'=>$dutyId))->getField('level');
        // $map['role_level'] = $dutyLvl;
        $map['emp_id'] = $this->cur_emp_id;
        // $map['role_deptid'] = $deptId; // array('in',array($deptId,$tar_deptId));

        $roles = D('OaUserRoleView')->where($map)->select();
        // var_dump($roles);
        // var_dump($operations);
        $index = false;
        $tmpRole = $roles[0];
        foreach ($roles as $role) {
            $tmp = array_search((int)$role['role_id'],$operations);
            // var_dump('find_first_step:role='.$role['role_id'].' tmpIndex=');
            // var_dump($tmp);
            // var_dump('find_first_step2: '.$dutyLvl.','.$role['role_level']);
            if ($tmp !== false) {
                if($dutyLvl == $role['role_level']) {
                    $index = $tmp + 1;
                    // var_dump($tmp);
                    break;
                }else if ($tmpRole['role_level'] < $role['role_level']){
                    $tmpRole = $role;
                    $index = $tmp + 1;
                }
            }
        }
        // var_dump($index);
        return $index;
    }

    private function check_apply_len($apply_data) {
        $flow_id = $apply_data['flow_id'];
        $flow = M('OaProcessFlow')->where(array('id'=>$flow_id))->find();
        $flowAttr = json_decode($flow['attr'],true);
        $map['application_id'] = $apply_data['id'];
        $alllog = M('OaProcessLog')->where($map)->order('time desc')->select();
        // var_dump($alllog);
        $logs = array();
        for($i = 0; $i < count($alllog); $i++) {
            if ($alllog[$i]['result'] == 0) {
                break;
            }else {
                array_push($logs,$alllog[$i]);
            }
        }
        // var_dump('check_apply_len');
        // var_dump($logs);
        $steps = array();
        foreach ($logs as $log) {
            if(!in_array($log['step'],$steps)) {
                array_push($steps,$log['step']);
            }
        }
        $cur_len = count($steps);
        // var_dump('check_apply_len:'.count($steps).','.$flowAttr['len']);
        if (($cur_len + 1) < $flowAttr['len']){
            return true;
        }else {
            return false;
        }
    }

    private function findNextApprover(&$apply_data,$operations) {
        $cur_step = $apply_data['step'] - 1;

        if ($cur_step >= count($operations)-1) {
            // var_dump('findNextApprover-cur-step:'.$cur_step.'operLen:'.count($operations));
            $apply_data['approver'] = $this->cur_emp_id;
            $apply_data['status'] = 1;
            $data['roleId'] = $operations[$cur_step-1];
            $data['emps'] = array($this->cur_emp_id);
            return $data;
        }
        for($i = $cur_step; $i < count($operations); $i++){
            $tmp = whoHasTheRole($operations[$i]);
            // var_dump('findNextApprover:'.$operations[$i],$tmp);
            if ($tmp['count'] == 0) {
                continue;
            } else if ($tmp['count'] == 1) {
                $apply_data['approver'] = $tmp['emps'][0]['emp_id'];
                // var_dump('findNextApprover-approver:'. $apply_data['approver']);
                $apply_data['step'] = $i+1;
                $data['roleId'] = $operations[$i];
                $data['emps'] = array($apply_data['approver']);
                return  $data;
            } else {
                $apply_data['approver'] = 0;
                $apply_data['step'] = $i+1;
                // var_dump('findNextApprover-Multi-approver:'. $apply_data['step']);
                $data['roleId'] = $operations[$i];
                $data['emps'] = array_column($tmp['emps'], 'emp_id');
                return $data;
            }
        }
        return false;
    }
    private function go_next_step ($apply_data, $old_data, $comment) {
        if ($old_data['step'] == 1) {
            $flowId = $this->chooseFlow($apply_data);
            if($flowId === false) {
                $return['errcode'] = '61451';
                $retrun['data'] = null;
                $return['errmsg'] = error_msg($return, '找不到能处理的流程！！');
                return $return;
            }
            $apply_data['flow_id'] = $flowId;

            $flow = M('OaProcessFlow')->where(array('id'=>$flowId))->find();
            $operations = json_decode($flow['operations'],true);

            $first_step = $this->find_first_step($operations,$old_data['applyer']);
            if($first_step === false) {
                $return['errcode'] = '61451';
                $retrun['data'] = null;
                $return['errmsg'] = error_msg($return, '员工职务角色有误！！');
                return $return;
            }
            $first_NextStep = $first_step + 1;
            $apply_data['step'] = $first_NextStep;
            // var_dump($apply_data['step']);
        }else {
            $flowId = $apply_data['flow_id'];
            $flow = M('OaProcessFlow')->where(array('id'=>$flowId))->find();
            $operations = json_decode($flow['operations'],true);
        }
        $res = $this->check_apply_len($apply_data);
        if($res === false) {
            $apply_data['status'] = 1;
            $apply_status = 1;
            $empList = [];
        }else {
            // var_dump('find next step!!');
            $res = $this->findNextApprover($apply_data,$operations);
            if ($res === false) {
                /*
                $return['errcode'] = '61451';
                $retrun['data'] = null;
                $return['errmsg'] = error_msg($return, '没有找到审批人');
                return $return;
                */
                // var_dump('can not find approver !!');
                $apply_data['status'] = 1;
                $apply_status = 1;
                $empList = [];
            }
            $empList = $res['emps'];
        }

        $apply_data['hops'] = $old_data['hops'] + 1;
        $apply_data['is_read'] = false;

        if ($apply_data['status'] == 1) {
            $apply_status = $apply_data['status'];
            $apply_data['status'] = 0;
        }
        // var_dump($apply_data);
        $roleName = M('OaRole')->where(array('id'=>$operations[$old_data['step']-1]))->getField('name');
        $apply_map['id'] = $apply_data['id'];

        if(M('OaApplication')->where($apply_map)->save($apply_data)){
            $log['appid'] = $this->appid;
            $log['emp_id'] = $apply_data['applyer'];
            $log['application_id'] = $apply_data['id'];
            $log['approver'] = $this->cur_emp_id;
            $log['step'] = $old_data['step'];
            $log['hop'] = $old_data['hops']+1;
            $log['status'] = 1;
            $log['result'] = true;
            $log['comment'] = $comment;
            $log['time'] = NOW_TIME;
            $log['node_name'] = $roleName;
            $em_map['id'] = $log['approver'];
            $man = M('OaEmployee')->where($em_map)->find();
            $log['approver_name'] = $man['name'];
            M('OaProcessLog')->add($log);
            foreach ( $empList as $emp) {
                D('OaEmpMsg')->sendApprovingMsg($emp,$apply_data,true);
            }
            $apply_data['node_name'] = $roleName;
            $apply_data['statusStr'] = '通过';
            if ($apply_data['applyer'] != $this->cur_emp_id){
                $apply_data['approver'] = $this->cur_emp_id;
                $apply_data['approver_name'] = $man['name'];
                D('OaEmpMsg')->sendApprovalResultMsg($apply_data['applyer'],$apply_data,true);
            }
        }

        if ($apply_status == 1) {
            $data['id'] = $operations[count($operations)-1];
            $lastName = M('OaRole')->where(array('id'=>$data['id']))->getField('name');
            array_push($flowRoles,$data);
            $this->attendanceApplyDone($apply_data['id'],count($operations),$lastName);
        }

        return true;
    }

    private function approvalFail_goback($comment,$old_data){
        $flow_id = $old_data['flow_id'];
        $flow = M('OaProcessFlow')->where(array('id'=>$flow_id))->find();
        $operations = json_decode($flow['operations'],true);
        $cur_role = $operations[$old_data['step']-1];
        $roleName = M('OaRole')->where(array('id'=>$cur_role))->getField('name');

        $old_step = $old_data['step'];
        $old_data['step'] = 1;
        $old_data['approver'] = $old_data['applyer'];
        $old_data['hops'] = $old_data['hops'] + 1;
        $old_data['is_read'] = true;
        $apply_map['id'] = $old_data['id'];
        if(M('OaApplication')->where($apply_map)->save($old_data)) {
            $log['appid'] = $this->appid;
            $log['emp_id'] = $old_data['applyer'];
            $log['application_id'] = $old_data['id'];
            $log['approver'] = $this->cur_emp_id;
            $log['step'] = $old_step;
            $log['hop'] = $old_data['hops'];
            $log['status'] = 1;
            $log['result'] = false;
            $log['comment'] = $comment;
            $log['time'] = NOW_TIME;
            $log['node_name'] = $roleName;
            $em_map['id'] = $log['approver'];
            $man = M('OaEmployee')->where($em_map)->find();
            $log['approver_name'] = $man['name'];
            M('OaProcessLog')->add($log);
            $apply_data = $old_data;
            $apply_data['node_name'] = $roleName;
            $apply_data['statusStr'] = '驳回';
            $apply_data['approver'] = $this->cur_emp_id;
            $apply_data['approver_name'] = $man['name'];
            return D('OaEmpMsg')->sendApprovalResultMsg($old_data['approver'],$apply_data,true);
        }

        return true;
    }

    private function process_attendance_apply($apply_data,$cur_step,$go_next,$comment) {
        if (empty($apply_data['id'])) {
            $return['errcode'] = '61451';
            $return['data'] = null;
            $return['errmsg'] = error_msg($return, 'apply_id 数据错误');
            return $return;
        }
        $apply_map['id'] = $apply_data['id'];
        $old_data = M('OaApplication')->where($apply_map)->find();
        if ($old_data['step'] != $cur_step) {
            $return['errcode'] = '61451';
            $return['data'] = null;
            $return['errmsg'] =error_msg($return, 'step 数据错误');
            return $return;
        }
        if($old_data['status'] == 1) {
            $return['errcode'] = '61451';
            $return['data'] = null;
            $return['errmsg'] =error_msg($return, '此单已完成！！');
            return $return;
        }

        if ($go_next == 0) {
            if ($cur_step == 1) {
                $res = M('OaApplication')->where($apply_map)->save($apply_data);
            } else {
                $tmp_data['approver'] = $apply_data['approver'];
                $tmp_data['is_read'] = $apply_data['is_read'];
                $res = M('OaApplication')->where($apply_map)->save($tmp_data);
            }
            if ($res === false) {
                $return['errcode'] = '61451';
                $return['errmsg'] = error_msg($return, '保存数据失败');
                $return['data'] = null;
                return $return;
            }else{
                return true;
            }
        } else if ($go_next == 1) {
            return $this->go_next_step($apply_data, $old_data, $comment);
        }else if ($go_next == -1) {
            return $this->approvalFail_goback($comment,$old_data);
        }
    }

    public function UpdateAttendceApply() {
        if(!$this->initial_check_pass) {
            return;
        }
        $cur_step = I('step');
        $comment = I('comment');

        $apply_data = json_decode(I('data'),true);
        $go_next = empty(I('go_next'))?0:I('go_next');

        if ($apply_data['type'] == 1) {
            $res = $this->process_attendance_apply($apply_data,$cur_step,$go_next,$comment);
        } else if ($apply_data['type'] == 2) {
            $res = $this->process_leaveTime_application($apply_data,$cur_step,$go_next,$comment);
        }

        if($res !== true){
            echo api_return( $res['errcode'],$res['data'],$res['errmsg']);
        }else{
            $apply_map['id'] = $apply_data['id'];
            $ret_data = M('OaApplication')->where($apply_map)->find();
            echo api_return( 0,$ret_data);
        }
    }

    private function attendanceApplyDone($apply_id,$lastStep,$lastName) {
        $old_data = M('OaApplication')->where(array('id'=>$apply_id))->find();
        $nodeName =$lastName;
        $old_data['status'] = true;
        $old_data['approve_date'] = NOW_TIME+1;
        $old_data['approver'] = 0;
        $old_data['step'] = $lastStep;
        $old_data['is_read'] = false;
        $old_data['hops'] = $old_data['hops']+1;
        // var_dump('attendanceApplyDone');
        if(M('OaApplication')->where(array('id'=>$apply_id))->save($old_data)){
            $log['appid'] = $this->appid;
            $log['emp_id'] = $old_data['applyer'];
            $log['application_id'] = $old_data['id'];
            $log['approver'] = $this->cur_emp_id;
            $log['step'] = $old_data['step'];
            $log['hop'] = $old_data['hops'];
            $log['status'] = 1;
            $log['result'] = true;
            $log['comment'] = '完成';
            $log['time'] = NOW_TIME+1;
            $log['node_name'] = $nodeName;
            $em_map['id'] = $log['approver'];
            $man = M('OaEmployee')->where($em_map)->find();
            $log['approver_name'] = $man['name'];
            M('OaProcessLog')->add($log);
            // D('OaEmpMsg')->sendmsg($appid,$formId,3,$old_data['applyer'],msg_reimbursementDone($old_data));
        }
        return true;
    }
    public function AttendceApplyDone() {
        if(!$this->initial_check_pass) {
            return;
        }
        echo api_return(0,null);
    }

    public function GetWorkTimeSetting(){
        if(!empty(I('year'))){
            $workTimeSetting = db_find('oa_work_time_setting', ['year'=>I('year')]);
            echo api_return(0,$workTimeSetting);
        } else {
            $workTimeSettings = M('OaWorkTimeSetting')->select();
            echo api_return(0,$workTimeSettings);
        }
    }
    public function GetListApplyForLeave(){
        $map['type'] = 2;
        $map['applyer'] = $this->cur_emp_id;
        $map['is_del'] = 0;

        if(!empty(I('status'))){
            $map['status'] = intval(I('status'));
        }
        if(!empty(I('page'))){
            $p = I('page');
            $row = empty(I('row'))?20:I('row');
            $count = M('OaApplication')->where($map)->count();
            $tmps = M('OaApplication')->where($map)->order('submit_date desc')->page($p,$row)->select();
            $data['count'] = $count;
            $data['data'] = $tmps;
        }else{
            $data = M('OaApplication')->where($map)->order('submit_date desc')->select();
        }

        echo api_return(0,$data);
    }
    public function GetApplyForLeave()
    {
       $map['id'] = I('apply_id');
       $map['type'] = 2;
       $data = M('OaApplication')->where($map)->find();
       echo api_return(0, $data);
    }
    public function GetListApplyForWorkOverTime(){
        $map['applyer'] = $this->cur_emp_id;
        $map['type'] = 1;
        $map['is_del'] = 0;
        if(!empty(I('status'))){
            $map['status'] = intval(I('status'));
        }
        if(!empty(I('page'))){
            $p = I('page');
            $row = empty(I('row'))?20:I('row');
            $count = M('OaApplication')->where($map)->count();
            $tmps = M('OaApplication')->where($map)->order('submit_date desc')->page($p,$row)->select();
            $data['count'] = $count;
            $data['data'] = $tmps;
        }else {
            $data = M('OaApplication')->where($map)->order('submit_date desc')->select();
        }
        echo api_return(0,$data);
    }
    public function GetApplyForWorkOverTime(){
        if(!empty(I('applyer'))){
            $map['applyer'] = I('applyer');
        }
        $map['id'] = I('apply_id');
        $map['type'] = 1;

        $data = M('OaApplication')->where($map)->find();
        echo api_return(0,$data);
    }

    public function GetApplyLeaveDoneList () {
        if (empty(I('startTime')) || I('endTime')){
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return));
        }

        $tmp_map['start_time'] = array('between',array(I('startTime'),I('endTime')));
        $tmp_map['end_time'] = array('between',array(I('startTime'),I('endTime')));
        $tmp_map['_logic'] = "or";
        $apply_map['_complex'] = $tmp_map;
        $apply_map['appid'] = $this->appid;
        $apply_map['applyer'] = $this->cur_emp_id;
        $apply_map['type'] = 2;
        $apply_map['status'] = 1;
        $apply_map['_logic'] = 'and';
        $leaves = M('OaApplication')->where($apply_map)->order('submit_date desc')->field('id,start_time,end_time')->select();
        echo api_return(0,$leaves);
    }

    private function process_leaveTime_application($apply_data,$cur_step,$go_next,$comment) {
        if (empty($apply_data['id'])) {
            $return['errcode'] = '61451';
            $retrun['data'] = null;
            $return['errmsg'] = error_msg($return, 'apply_id 数据错误');
            return $return;
        }
        $apply_map['id'] = $apply_data['id'];
        $old_data = M('OaApplication')->where($apply_map)->find();
        if ($old_data['step'] != $cur_step) {
            $return['errcode'] = '61451';
            $return['data'] = null;
            $return['errmsg'] =error_msg($return, 'step 数据错误');
            return $return;
        }
        if($old_data['status'] == 1) {
            $return['errcode'] = '61451';
            $return['data'] = null;
            $return['errmsg'] =error_msg($return, '此单已完成！！');
            return $return;
        }

        if ($go_next == 0) {
            if ($cur_step == 1) {
                $res = M('OaApplication')->where($apply_map)->save($apply_data);
            } else {
                $tmp_data['approver'] = $apply_data['approver'];
                $tmp_data['is_read'] = $apply_data['is_read'];
                $res = M('OaApplication')->where($apply_map)->save($tmp_data);
            }
            if ($res === false) {
                $return['errcode'] = '61451';
                $return['errmsg'] = error_msg($return, '保存数据失败');
                $return['data'] = null;
                return $return;
            }else{
                return true;
            }
        } else if ($go_next == 1) {
            return $this->leaveApplication_go_next_step($apply_data, $old_data, $comment);
        }else if ($go_next == -1) {
            return $this->leaveApplication_approvalFail_goback($comment,$old_data);
        }

        return true;
    }
    private function leaveApplication_chooseFlow(&$apply_data) {
        $totalTime = $apply_data['total_money'];
        $empInfo = D('OaEmployeeDeptView')->where(array('id'=>$this->cur_emp_id))->find();
        $flows = M('OaProcessFlow')->where(array('type'=>$apply_data['type']))->select();  // 休假流程
        $flowid = false;
        foreach ($flows as $flow) {
            $attr = json_decode($flow['attr'],true);
            if ($attr['dept'] != intval($empInfo['dept_id'])) {
                continue;
            }

            if (!in_array(intval($empInfo['duty_lvl']), $attr['duty_lvl'])) {
                // var_dump("leaveApplication_chooseFlow 1");
                continue;
            }
            if (($attr['days']['max'] > 0) && ($attr['days']['max'] * 24 * 60 * 60 >= $totalTime)) {
                // 3 天以内包括3天
                $apply_data['flow_id'] = $flow['id'];
                // $apply_data['sub_type'] = 1;
                break;
            } else if (($attr['days']['max'] < 0) && ($attr['days']['min']* 24 * 60 * 60 < $totalTime)){
                // 3 天以上
                $apply_data['flow_id'] = $flow['id'];
                // $apply_data['sub_type'] = 2;
                break;
            }
        }
        return $apply_data['flow_id'];
    }

    private function leaveApplication_go_next_step($apply_data, $old_data, $comment) {
        if ($old_data['step'] == 1) {
            if(empty($apply_data['total_money'])) {
                $return['errcode'] = '61451';
                $return['data'] = null;
                $return['errmsg'] = error_msg($return, 'total time can not be 0 nor empty！！');
                return $return;
            }
            $flowId = $this->leaveApplication_chooseFlow($apply_data);
            if ($flowId === false) {
                $return['errcode'] = '61451';
                $return['data'] = null;
                $return['errmsg'] = error_msg($return, '找不到能处理的流程！！');
                return $return;
            }
            $apply_data['flow_id'] = $flowId;
        }

        $flowId = $apply_data['flow_id'];
        $flow = M('OaProcessFlow')->where(array('id'=>$flowId))->find();
        $operations = json_decode($flow['operations'],true);

        $nextStep = $apply_data['step'];
        if ($nextStep >= count($operations)) {
            $apply_data['approver'] = 0;
        }else {
            $roleId = $operations[$apply_data['step']-1];
            $tmp = whoHasTheRole($roleId);
            if($tmp['count'] == 0) {
                $return['errcode'] = '61451';
                $return['data'] = null;
                $return['errmsg'] = error_msg($return, '无休假审批人！！');
                return $return;
            }else if($tmp['count'] == 1){
                $apply_data['approver'] = $tmp['emps'][0]['emp_id'];
            } else {
                $apply_data['approver'] = 0;
            }
        }

        $roleName = M('OaRole')->where(array('id'=>$operations[$old_data['step']-1]))->getField('name');
        $apply_map['id'] = $apply_data['id'];
        $apply_data['hop'] = $old_data['hop'] + 1;
        if(M('OaApplication')->where($apply_map)->save($apply_data)){
            $log['appid'] = $this->appid;
            $log['emp_id'] = $apply_data['applyer'];
            $log['application_id'] = $apply_data['id'];
            $log['approver'] = $this->cur_emp_id;
            $log['step'] = $old_data['step'];
            $log['hop'] = $old_data['hops']+1;
            $log['status'] = 1;
            $log['result'] = true;
            $log['comment'] = $comment;
            $log['time'] = NOW_TIME;
            $log['node_name'] = $roleName;
            $em_map['id'] = $log['approver'];
            $man = M('OaEmployee')->where($em_map)->find();
            $log['approver_name'] = $man['name'];
            M('OaProcessLog')->add($log);
            foreach ( $empList as $emp) {
                D('OaEmpMsg')->sendApprovingMsg($emp,$apply_data,true);
            }
            $apply_data['node_name'] = $roleName;
            $apply_data['statusStr'] = '通过';
            if ($apply_data['applyer'] != $this->cur_emp_id){
                $apply_data['approver'] = $this->cur_emp_id;
                $apply_data['approver_name'] = $man['name'];
                D('OaEmpMsg')->sendApprovalResultMsg($apply_data['applyer'],$apply_data,true);
            }
        }

        if ($nextStep >= (count($operations))) {
            $roleId = $operations[count($operations)-1];
            $lastName = M('OaRole')->where(array('id'=>$roleId))->getField('name');
            $this->attendanceApplyDone($apply_data['id'],count($operations),$lastName);
        }
        return true;
    }
    private function leaveApplication_approvalFail_goback($comment,$old_data){
        return $this->approvalFail_goback($comment,$old_data);
    }
}
